package com.gf.help.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.gf.common.bean.Page;
import com.gf.help.dao.QuestionDao;
import com.gf.help.dao.ReplyDao;
import com.gf.help.model.Reply;
import com.gf.help.service.ReplyService;
@Transactional
@Service("replyService")
public class ReplyServiceImpl implements ReplyService {
	@Autowired
	private ReplyDao replyDao;
	@Autowired
	private QuestionDao questionDao;

	@Override
	public Page getRepliesByQuestionId(long questionId, int pageSize,
			int currentPage) {
		Page page = new Page();
		page.setCurpage(currentPage);
		page.setPageSize(pageSize);
		int total = replyDao.queryCountByQuestionId(questionId);
		page.setTotal(total);
		List<Reply> replies = replyDao.queryRepliesByQuestionId(questionId,
				pageSize, currentPage);
		page.setResults(replies);
		return page;
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public boolean save(Reply reply) {
		long questionId= reply.getQuestionId();
		int replyCount=replyDao.queryCountByQuestionId(questionId);
		boolean tag=questionDao.updateReplyCount(reply.getQuestionId(), replyCount+1);
		return replyDao.insert( reply.getReplierId(),
				reply.getReplierName(),questionId,
				reply.getContent(),reply.getReplierPortrait())&&tag;
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public boolean delete(long id) {
		return replyDao.delete(id);
	}
	@Transactional(propagation=Propagation.REQUIRED,rollbackFor=Exception.class)
	@Override
	public boolean update(String content, long id) {
		return replyDao.update(content, id);
	}

}
